class Sqlc < Formula
  desc "Generate type safe Go from SQL"
  homepage "https://sqlc.dev/"
  url "https://github.com/kyleconroy/sqlc/archive/v1.12.0.tar.gz"
  sha256 "36f9dc2013663267a0dc1d6bda22d4698eddebd4e72ab842e99bf51e7d60a89f"
  license "MIT"
  head "https://github.com/kyleconroy/sqlc.git", branch: "main"

  bottle do
    sha256 cellar: :any_skip_relocation, arm64_monterey: "70b41300997477c91eddeb66605f9d433cae096404f3ae841bce0dc0f3422b65"
    sha256 cellar: :any_skip_relocation, arm64_big_sur:  "d392480189ba365e4fea0fd8e177349299dee9594784bcc84084dfb3fab965b7"
    sha256 cellar: :any_skip_relocation, monterey:       "af17ee61912707c52ead8531f01483f7a4d63be5feca1553e67bb8b3e6fd24d1"
    sha256 cellar: :any_skip_relocation, big_sur:        "b7b25f51a5b5fd77af11dbed43b0a2853ff46aed33d074d973a461460bd0a877"
    sha256 cellar: :any_skip_relocation, catalina:       "61b03813a446443c89ab9c0b7d82557e44656353e8de2a234285d016828c29a4"
    sha256 cellar: :any_skip_relocation, x86_64_linux:   "66e9eeab6866454ba422559f7dfff9d9e5f981091c9a46e8e5df3c5aaff87809"
  end

  depends_on "go" => :build

  def install
    system "go", "build", *std_go_args, "-ldflags", "-s -w", "./cmd/sqlc"
  end

  test do
    (testpath/"sqlc.json").write <<~SQLC
      {
        "version": "1",
        "packages": [
          {
            "name": "db",
            "path": ".",
            "queries": "query.sql",
            "schema": "query.sql",
            "engine": "postgresql"
          }
        ]
      }
    SQLC

    (testpath/"query.sql").write <<~EOS
      CREATE TABLE foo (bar text);

      -- name: SelectFoo :many
      SELECT * FROM foo;
    EOS

    system bin/"sqlc", "generate"
    assert_predicate testpath/"db.go", :exist?
    assert_predicate testpath/"models.go", :exist?
    assert_match "// Code generated by sqlc. DO NOT EDIT.", File.read(testpath/"query.sql.go")
  end
end
